package waggle.server;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import waggle.client.services.OccurrenceService;
import waggle.client.bean.User;
import waggle.client.bean.Role;
import waggle.client.bean.Email;
import waggle.client.bean.EmailException;
import waggle.client.bean.Occurrence;
import waggle.client.bean.UserExistedException;
import waggle.client.bean.OccurrenceComments;
import waggle.client.UserQuery;
import waggle.client.OccurrenceQuery;
import waggle.client.bean.OccurrenceReview;
import waggle.client.OccurrenceCommentQuery;

/**
 * The default implementation of {@link OccurrenceService}. Uses
 * {@link OccurrenceDb} service to fetch {@link Occurrence} objects from the
 * database.
 * 
 */
@SuppressWarnings("serial")
public class OccurrenceServiceImpl extends RemoteServiceServlet implements
		OccurrenceService {

	  /**
	   * The {@link OccurrenceDb} used for querying {@link Occurrence} objects.
	   */
	  OccurrenceDb occurrenceDb = DBFactory.getOccurrenceDb();
	
	  
	  /**
	   * The {@link SessionIdService} used for validating session ids.
	   */
	  // @Inject
	  SessionIdService sessionService = DBFactory.getSessionIdService();
	  
	
	// ??

	/**
	 * Deletes all {@link Occurrence} found by given query and belong to the
	 * given user, found by session id.
	 * 
	 * @see org.rebioma.client.services.OccurrenceService#delete(java.lang.String,
	 *      org.rebioma.client.OccurrenceQuery)
	 */
	public int delete(String sessionId, OccurrenceQuery query)
			throws OccurrenceServiceException {
		return 0;
	}

	public Map<Integer, Boolean> getMyReviewedOnRecords(String sid,
			Map<Integer, Integer> rowOccIdsMap)
			throws OccurrenceServiceException {
		return null;
	}

	@Override
	public List<OccurrenceReview> getOccurrenceReviewsOf(Integer occId)
			throws OccurrenceServiceException {
		return null;
	}

	/**
	 * @see org.rebioma.client.services.OccurrenceService#lastUpdateInMilliseconds()
	 */
	public Long lastUpdateInMilliseconds() throws OccurrenceServiceException {
		return null;
	}

	public int reviewRecords(String sid, Boolean reviewed,
			OccurrenceQuery query, String comment)
			throws OccurrenceServiceException {
		return 0;
	}

	public int reviewRecords(String sid, Boolean reviewed,
			Set<Integer> occurrenceIds, String comment)
			throws OccurrenceServiceException {
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.rebioma.client.services.OccurrenceService#update(java.lang.String,
	 * org.rebioma.client.OccurrenceQuery)
	 */
	public Integer update(String sessionId, OccurrenceQuery query)
			throws OccurrenceServiceException {
		return null;
	}

	public int updateComments(String sessionId, Set<OccurrenceComments> comments)
			throws OccurrenceServiceException {
		return 0;
	}

	private int reviewRecords(User user, Boolean reviewed,
			Collection<Integer> occurrenceIds, String comment) {
		return 0;
	}

	// occurrence
	/**
	 * (non-Javadoc)
	 * 
	 * @see org.rebioma.client.services.OccurrenceService#delete(java.lang.String,
	 *      java.util.Set)
	 */
	public String delete(String sessionId, Set<Occurrence> occurrences)
			throws OccurrenceServiceException {
		return null;
	}

	/**
	 * Fetches a set of {@link Occurrence} objects that match the query. Throws
	 * an exception if the session id is invalid or if there's a problem
	 * querying the database.
	 * 
	 */
	public OccurrenceQuery fetch(String sessionId, OccurrenceQuery query)
			throws OccurrenceServiceException {

//kayjean
	    User user = sessionService.getUserBySessionId(sessionId);
//	    Integer userId = null;
//	    if (user == null) {
//	      enforcePublicQuery(query);
//	    } else {
//	      userId = user.getId();
//	    }
//	    try {
//	      List<Occurrence> results = occurrenceDb
//	          .findByOccurrenceQuery(query, user);
//	      query.setResults(results);
//	      return query;
//	    } catch (Exception e) {
//	      e.printStackTrace();
//	      throw new OccurrenceServiceException("Unable to fetch: " + e);
//	    }
		
	    try {
	        List<Occurrence> results = occurrenceDb
	            .findByOccurrenceQuery(query, user);
	        query.setResults(results);
	        return query;
	      } catch (Exception e) {
	        e.printStackTrace();
	        throw new OccurrenceServiceException("Unable to fetch: " + e);
	      }
	}
	
	/**
	 * Updates the set of {@link Occurrence} objects. If the session is invalid,
	 * returns null.
	 * 
	 * @throws OccurrenceServiceException
	 * @see org.rebioma.client.services.OccurrenceService#update(java.lang.String,
	 *      java.util.Set)
	 */
	public String update(String sessionId, Set<Occurrence> occurrences)
			throws OccurrenceServiceException {
		return null;
	}

	// comment
	public Integer deleteComments(String sid, Set<OccurrenceComments> comments)
			throws OccurrenceServiceException {
		return null;
	}

	public OccurrenceCommentQuery fetch(OccurrenceCommentQuery query)
			throws OccurrenceServiceException {
		
	    try {
	        List<OccurrenceComments> results = occurrenceDb
	            .findByOccurrenceQuery(query);
	        query.setResults(results);
	        return query;
	      } catch (Exception e) {
	        e.printStackTrace();
	        throw new OccurrenceServiceException("Unable to fetch: " + e);
	      }
	}

}
